package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.SysCommDto;
import com.ysd.lis.dto.SysCommParam;
import com.ysd.lis.entity.SysCampusArea;
import com.ysd.lis.mapper.sys.SysCampusAreaMapper;
import com.ysd.lis.service.SysCampusAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-02-20
 */
@Service
public class SysCampusAreaServiceImpl extends ServiceImpl<SysCampusAreaMapper, SysCampusArea> implements SysCampusAreaService {
    @Autowired
    SysCampusAreaMapper sysCampusAreaMapper;

    @Override
    public Result findCampusAreaList(SysCommParam param) {
        MPJLambdaWrapper<SysCampusArea> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(SysCampusArea::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SysCampusArea::getCampusName, param.getSeachValue())
                    .or().like(SysCampusArea::getCampusCode, param.getSeachValue())
                    .or().like(SysCampusArea::getInputCode,param.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(SysCampusArea::getCreateTime);
        List<SysCampusArea> supplierPage = sysCampusAreaMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result findCampusAreaPageList(SysCommParam param) {
        Page<SysCampusArea> page = new Page<>();
        page.setCurrent(param.getPageIndex());
        page.setSize(param.getPageSize());
        MPJLambdaWrapper<SysCampusArea> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(SysCampusArea::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SysCampusArea::getCampusName, param.getSeachValue())
                    .or().like(SysCampusArea::getCampusCode, param.getSeachValue())
                    .or().like(SysCampusArea::getInputCode,param.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(SysCampusArea::getCreateTime);
        Page<SysCampusArea> supplierPage = sysCampusAreaMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addCampusArea(SysCampusArea sysCampusArea) {
        int insert = sysCampusAreaMapper.insert(sysCampusArea);
        if(insert>0){
            return Result.succ(1,"添加成功",sysCampusArea);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editCampusArea(SysCampusArea supplier) {
        int updateById = sysCampusAreaMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteCampusArea(SysCommDto dto) {
        List<SysCampusArea> supplierList = dto.getCampusAreaList();
        List<String> ids = supplierList.stream().map(SysCampusArea::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
